<?php

namespace app\admin\model;

use think\Model;

class FightDetail extends Model
{
    private static $instance = null;

//    public function getAddTimeAttr($time)
//    {
//        return date('Y-m-d H:i:s', $time);
//    }

    public static function getInstance()
    {
        //检测当前类属性$instance是否已经保存了当前类的实例
        if (self::$instance == null) {
            //如果没有,则创建当前类的实例
            self::$instance = new self();
        }
        //如果已经有了当前类实例,就直接返回,不要重复创建类实例
        return self::$instance;
    }

    /**
     * 单条查询
     * @param $where
     * @param string $field
     * @param false $lock
     * @return Member
     */
    public function getOne($where, $field = '*', $order = 'add_time', $lock = false)
    {
        return $this->lock($lock)->field($field)->order($order, 'desc')->where($where)->find();
    }

    // 用户列表
    public function memberList($where, $page, $size, $field = [], $order = 'dinosaur_num')
    {
        $list = $this->field($field)
            ->where($where)
            ->order($order, 'desc')
            ->paginate($size, true, ['page' => $page])
            ->toArray();

        return $list['data'];
    }

    public function getAll($where, $field = '*')
    {
        return $this->field($field)->where($where)->select();
    }

    public function getListByPage($where, $page, $size, $field = [])
    {
        $list = $this->field($field)
            ->where($where)
            ->order('add_time', 'desc')
            ->paginate($size, true, ['page' => $page])
            ->toArray();

        return $list['data'];
    }

    public function getCount($where = [], $field = '*')
    {
        return $this->where($where)->count($field);
    }

    public function getList($num = 15)
    {
        $keywords = input('param.keywords', '');
        $range = input('param.range', '');
        $monster_type = input('param.monster_type');
        $dinosaur_type = input('param.dinosaur_type');
        $where = "1=1";
        $param = [];
        if ($keywords) {
            $param['keywords'] = $keywords;
            $where .= " and user_address like '" . $keywords . "%'";
        }
        if ($range) {
            $fight_times = explode(" - ", $range);
            $fight_start_time = $fight_times[0] . " 00:00:00";
            $fight_ends_time = $fight_times[1] . " 23:59:59";
            $param['range'] = $range;
            $where .= " and add_time >= '{$fight_start_time}' and add_time <= '{$fight_ends_time}'";
        }
        if ($monster_type != null && $monster_type != -1) {
            $param['monster_type'] = $monster_type;
            $where .= " and monster_level_id = " . $monster_type;
        }
        if ($dinosaur_type != null && $dinosaur_type != -1) {
            $param['dinosaur_type'] = $dinosaur_type;
            $where .= " and dinosaur_level_id = " . $dinosaur_type;
        }
//        halt($where);
        $exchangeList = $this->where($where)
            ->order('id', 'DESC')
            ->paginate($num, false, ['query' => request()->param()]);
        if ($exchangeList) {
            $page = $exchangeList->render();// 获取分页显示
            return ['data' => $exchangeList, 'msg' => '数据查询成功', 'page' => $page];
        } else {
            return ['data' => '', 'msg' => '暂无数据', 'page' => ''];
        }
    }

    public function exportExcel()
    {
        $keywords = input('param.keywords', '');
        $range = input('param.range', '');
        $monster_type = input('param.monster_type');
        $dinosaur_type = input('param.dinosaur_type');
        $where = "1=1";
        $param = [];
        if ($keywords) {
            $param['keywords'] = $keywords;
            $where .= " and user_address like '" . $keywords . "%'";
        }
        if ($range) {
            $fight_times = explode(" - ", $range);
            $fight_start_time = $fight_times[0] . " 00:00:00";
            $fight_ends_time = $fight_times[1] . " 23:59:59";
            $param['range'] = $range;
            $where .= " and add_time >= '{$fight_start_time}' and add_time <= '{$fight_ends_time}'";
        }
        if ($monster_type != null && $monster_type != -1) {
            $param['monster_type'] = $monster_type;
            $where .= " and monster_level_id = " . $monster_type;
        }
        if ($dinosaur_type != null && $dinosaur_type != -1) {
            $param['dinosaur_type'] = $dinosaur_type;
            $where .= " and dinosaur_level_id = " . $dinosaur_type;
        }

        return $this->where($where)
            ->order('id', 'DESC')
            ->select()->toArray();
    }


    public function getPage($where = [], $page = 1, $size = 15, $order = 'id desc', $field = '*')
    {
        return $this->where($where)
            ->order($order)
            ->field($field)
            ->paginate($size, '', ['page' => $page])
            ->toArray();
    }

    public function getSum($where = [], $field = '*')
    {
        return $this->where($where)->sum($field);
    }

    public function getValues($where = [], $field = '*')
    {
        return $this->where($where)->value($field);
    }

    public function editByWhere($where, $data)
    {
        return $this->where($where)->update($data);
    }
}
